<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryParams.processDefinitionKey"
        :placeholder="'流程定义Key'"
        class="filter-item search-item"
      />
      <el-input
        v-model="queryParams.processDefinitionName"
        :placeholder="'流程定义Name'"
        class="filter-item search-item"
      />

      <el-button class="filter-item" type="primary" plain @click="search">
        查询
      </el-button>
      <el-button class="filter-item" type="warning" plain @click="reset">
        重置
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        plain
        @click="isVisible = !isVisible"
      >
        部署流程
      </el-button>
    </div>
    <el-dialog
      :title="title"
      width="21%"
      top="50px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="isVisible"
      ><el-upload
        :headers="headers"
        class="upload-demo"
        drag
        action="/activiti/service/definition/upload"
        multiple
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          只能上传png、bpmn和zip文件，且不超过500kb
        </div>
      </el-upload></el-dialog
    >
    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      style="width: 100%"
    >
      <el-table-column
        :label="'流程定义标识'"
        prop="id"
        :show-overflow-tooltip="true"
        align="center"
        min-width="120px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="'流程定义KEY'"
        prop="id"
        :show-overflow-tooltip="true"
        align="center"
        min-width="120px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.key }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'名称'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'版本'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.revision }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程描述'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.description }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'所属分类'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.category }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'部署时间'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.deployment.deploymentTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程定义'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.resourceName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程图'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{ scope.row.processDefinition.diagramResourceName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'流程定义状态'" class-name="status-col">
        <template slot-scope="scope">
          <span>{{
            scope.row.processDefinition.suspensionState ||
              state(scope.row.processDefinition.suspensionState)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="'操作'"
        align="center"
        min-width="200px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <i
            class="el-icon-edit table-operation"
            style="color: #2db7f5"
            @click="change(row)"
            >转换模型</i
          >
          <i
            class="el-icon-delete table-operation"
            style="color: #f50"
            @click="singleDelete(row)"
            >删除</i
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.num"
      :limit.sync="pagination.size"
      @pagination="fetch"
    />
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import Pagination from "@/components/Pagination";

export default {
  name: "processdefinition",
  components: { Pagination },
  filters: {
    state(value) {
      if (value == "1") {
        return "已激活";
      } else if (value == "2") {
        return "已挂起";
      }
    }
  },
  data() {
    return {
      title: "",
      headers: {
        Authorization: `bearer ${getToken()}`
      },
      isVisible: false,
      dialog: {
        isVisible: false,
        title: ""
      },
      userViewVisible: false,
      workflowVisible: false,
      tableKey: 0,
      loading: false,
      list: null,
      total: 0,
      queryParams: {},
      sort: {},
      selection: [],
      pagination: {
        size: 10,
        num: 1
      }
    };
  },
  computed: {
    currentUser() {
      return this.$store.state.account.user;
    }
  },
  // 生成页面
  mounted() {
    this.fetch();
  },
  methods: {
    change(row) {
      var queryObj = {
        processDefinitionId: row.processDefinition.id
      };
      this.$post(`activiti/service/definition/convertToModel`, {
        ...queryObj
      }).then(() => {
        this.$message({
          message: this.$t("转换成功"),
          type: "success"
        });
        this.search();
      });
    },
    search() {
      this.fetch({
        ...this.queryParams,
        ...this.sort
      });
    },
    reset() {
      this.queryParams = {};
      this.sort = {};
      this.$refs.table.clearSort();
      this.$refs.table.clearFilter();
      this.search();
    },
    singleDelete(row) {
      this.$refs.table.toggleRowSelection(row, true);
      this.batchDelete();
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t("tips.noDataSelected"),
          type: "warning"
        });
        return;
      }
      const contain = false;
      this.$confirm(this.$t("tips.confirmDelete"), this.$t("common.tips"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning"
      })
        .then(() => {
          const flowIds = [];
          this.selection.forEach(u => {
            flowIds.push(u.deployment.id);
          });
          this.delete(flowIds);
        })
        .catch(() => {
          this.clearSelections();
        });
    },
    delete(deploymentId) {
      this.loading = true;
      this.$get(
        `/activiti/service/definition/process/delete/${deploymentId}`
      ).then(() => {
        this.$message({
          message: this.$t("tips.deleteSuccess"),
          type: "success"
        });
        this.search();
      });
    },
    fetch(params = {}) {
      var _this = this;
      params.pageSize = _this.pagination.size;
      params.pageNum = _this.pagination.num;
      _this.loading = true;
      _this
        .$post("activiti/service/definition/findProcessDefinition", {
          ...params
        })
        .then(r => {
          const data = r.data.resultBody;
          _this.total = data.total;
          _this.list = data.records;
          _this.loading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped></style>
